<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>C Data Handles</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ffi.typeof.html">« FFI::typeof</a></li>
      <li style="float: right;"><a href="class.ffi-ctype.html">FFI\CType »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.ffi.html">FFI</a></li>
    <li>C Data Handles</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.ffi-cdata" class="reference">

 <h1 class="title">C Data Handles</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 7 &gt;= 7.4.0, PHP 8)</p>


  <div class="section" id="ffi-cdata.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    <span class="classname"><strong class="classname">FFI\CData</strong></span> objects can be used in a number of ways as a regular

    PHP data:
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       C data of scalar types can be read and assigned via the <span class="property">$cdata</span> property, e.g. 
       <code class="code">$x = FFI::new(&#039;int&#039;); $x-&gt;cdata = 42;</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C struct and union fields can be accessed as regular PHP object property, e.g.
       <code class="code">$cdata-&gt;field</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C array elements can be accessed as regular PHP array elements, e.g.
       <code class="code">$cdata[$offset]</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C arrays can be iterated using <a href="control-structures.foreach.html" class="link">foreach</a> statements.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C arrays can be used as arguments of <span class="function"><a href="function.count.html" class="function">count()</a></span>.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C pointers can be dereferenced as arrays, e.g. <code class="code">$cdata[0]</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C pointers can be compared using regular comparison operators (<code class="code">&lt;</code>,
       <code class="code">&lt;=</code>, <code class="code">==</code>, <code class="code">!=</code>, <code class="code">&gt;=</code>, <code class="code">&gt;</code>).
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C pointers can be incremented and decremented using regular <code class="code">+</code>/<code class="code">-</code>/
       <code class="code">++</code>/<code class="code">–-</code> operations, e.g. <code class="code">$cdata += 5</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C pointers can be subtracted from another using regular <code class="code">-</code> operations.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       C pointers to functions can be called as a regular PHP closure, e.g. <code class="code">$cdata()</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Any C data can be duplicated using the <a href="language.oop5.cloning.html" class="link">clone</a>
       operator, e.g. <code class="code">$cdata2 = clone $cdata;</code>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Any C data can be visualized using <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span>, <span class="function"><a href="function.print-r.html" class="function">print_r()</a></span>, etc.
      </span>
     </li>
    </ul>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      Notable limitations are that <span class="classname"><strong class="classname">FFI\CData</strong></span> instances do not support
      <span class="function"><a href="function.isset.html" class="function">isset()</a></span>, <span class="function"><a href="function.empty.html" class="function">empty()</a></span> and <span class="function"><a href="function.unset.html" class="function">unset()</a></span>,
      and that wrapped C structs and unions do not implement <span class="interfacename"><a href="class.traversable.html" class="interfacename">Traversable</a></span>.
     </span>
    </p></blockquote>
   </p>
  </div>


  <div class="section" id="ffi-cdata.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">final</span>
      <span class="modifier">class</span> <strong class="classname">FFI\CData</strong>
     </span>
     {</div>
   }</div>


  </div>

 </div>

 

</div>
</div></div></body></html>